<script lang="ts" setup>
import { validateNull, verifyUrl } from '@/utils/validate';
import type { PropType } from 'vue';
import { ref } from 'vue';
import type { DataInfoCard } from '@/api/data/types/dataInfoCard';

// 检查 props 数据
const props = defineProps({
  data: {
    type: Object || {} as () => DataInfoCard,
    required: true,
  },
  releaseInfo: {
    type: Object || {} as () => {
    },
    required: true,
  },
  pdfUrl: {
    type: String as PropType<string | null>,
    default: '',
  },
});

// console.log('接收到的 props 数据:', props); // 检查是否成功接收 props 数据

const activeSection = ref('description');
const pdfLoadError = ref(false);

const handlePdfLoadError = () => {
  pdfLoadError.value = true;
  // console.log('PDF 加载失败');
};
</script>

<template>
  <div class="info-card">
    <div class="info-card-nav">
      <button :class="{ active: activeSection === 'description' }" @click="activeSection = 'description'">
        数据集介绍
      </button>
      <button :class="{ active: activeSection === 'releaseInfo' }" @click="activeSection = 'releaseInfo'">
        发布信息
      </button>
      <button :class="{ active: activeSection === 'pdf' }" @click="activeSection = 'pdf'">
        阅读 PDF
      </button>
    </div>

    <div v-if="activeSection === 'description'" class="info-card-content">
      <!-- <h3 class="info-card-section-title">
        数据集介绍
      </h3> -->

      <div v-if="data != null && typeof data === 'object' && Object.keys(data).length > 0">
        <div class="info-card-header">
          <h2 class="info-card-title">
            {{ data.title }}
          </h2>
        </div>
        <p class="info-card-description">
          {{ data.description }}
        </p>
        <h3 class="info-card-section-title">
          {{ data.citationTitle }}
        </h3>
        <pre class="info-card-citation">{{ data.citation }}</pre>
        <h3 class="info-card-section-title">
          {{ data.datasetInfoTitle }}
        </h3>
        <pre class="info-card-dataset-info">{{ data.datasetInfo }}</pre>
        <h3 class="info-card-section-title">
          {{ data.acknowledgmentTitle }}
        </h3>
        <pre class="info-card-acknowledgment">{{ data.acknowledgment }}</pre>
      </div>

      <p v-else>
        暂无数据
      </p>
    </div>

    <div v-if="activeSection === 'releaseInfo'" class="info-card-content info-card-release-info">
      <h3 class="info-card-section-title">
        发布机构
      </h3>
      <p>{{ releaseInfo.organization }}</p>
      <h3 class="info-card-section-title">
        发布时间
      </h3>
      <p>{{ releaseInfo.releaseDate }}</p>
      <h3 class="info-card-section-title">
        发布首页
      </h3>
      <a :href="releaseInfo.releaseHomepage" target="_blank" rel="noopener noreferrer">{{ releaseInfo.releaseHomepage }}</a>
      <h3 class="info-card-section-title">
        发布链接
      </h3>
      <a :href="releaseInfo.releaseLink" target="_blank" rel="noopener noreferrer">{{ releaseInfo.releaseLink }}</a>
    </div>

    <div v-if="activeSection === 'pdf'" class="info-card-content">
      <h3 class="info-card-section-title">
        阅读 PDF 文档
      </h3>
      <iframe
        v-if="pdfUrl != null && !validateNull(pdfUrl) && verifyUrl(pdfUrl) && !pdfLoadError"
        :src="`${pdfUrl}`"
        width="100%"
        height="600rem"
        style="border: none"
        title="PDF 文档"
        @error="handlePdfLoadError"
      />

      <p v-else>
        无有效的 PDF 文档链接
      </p>
    </div>
  </div>
</template>

<style scoped>
.info-card {
  width: 100%;
  border-radius: 0.625rem; /* 10px */
  padding: 0 2.5rem;
  background-color: #fff;
  margin-bottom: 1.09375rem;
}

.info-card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.info-card-title {
  font-size: 1.3rem; /* 28px */
  font-weight: bold;
  margin: 0;
  color: #333333;
}

.info-card-nav {
  display: flex;
  margin-top: 0.875rem; /* 蓝色下划线 */
  border-bottom: 0.0625rem solid #ddd; /* 1px */
}

.info-card-nav button {
  margin-right: 0.2rem;
  font-size: 1rem; /* 20px */
  color: #333;
  background-color: transparent;
  padding: 0.7rem 0;
  border: none;
  cursor: pointer;
  outline: none;
  position: relative;
  margin-left: 1.2rem;
}

.info-card-nav button.active {
  color: #2468F2;
  border-bottom: 0.125rem solid #2468f2; /* 2px */
}

.info-card-nav button.active::after {
  content: '';
  position: absolute;
  bottom: -0.125rem; /* -2px */
  left: -1.25rem;
  right: -1.25rem;
  height: 0.125rem; /* 2px */
  background-color: #2468F2;
  border-bottom: 0.125rem solid #2468F2; /* 蓝色下划线 */
}

.info-card-nav button:hover {
  color: #0056b3;
}

.info-card-content {
  margin-top: 1.2rem;
}

.info-card-description {
  font-size: 0.9rem; /* 20px */
  line-height: 1.6;
  color: #555;
}

.info-card-section-title {
  font-size: 1.3rem; /* 28px */
  margin-top: 0.9075rem;
  margin-bottom: 0.8175rem;
  font-weight: bold;
  color:#333333;
}

.info-card-citation {
  font-size: 0.9rem; /* 20px */
  line-height: 1.5;
  background-color: #F2F5F9;
  padding: 0.625rem;
  border-radius: 0.25rem;
  white-space: pre-wrap;
}

.info-card-dataset-info {
  margin-left: -0.625rem;
  margin-top: -1%;
  margin-bottom: -1%;
  font-size: 0.9rem; /* 20px */
  line-height: 1.5;
  background-color: white;
  padding: 0.546875rem;
  border-radius: 0.25rem;
  white-space: pre-wrap;
}

.info-card-acknowledgment {
  margin-left: -0.68rem;
  margin-top: -0.655rem;
  font-size: 0.9rem; /* 20px */
  line-height: 1.5;
  background-color: white;
  padding: 0.625rem;
  border-radius: 0.25rem;
  white-space: pre-wrap;
}

/* src/components/InfoCard/InfoCard.css */

.info-card-release-info {
  line-height: 1.5;
  color: #333333;
}

.info-card-release-info h3 {
  margin-top: 1rem;
  margin-bottom: 0.5rem;
  font-size: 0.9rem; /* 28px */
  font-weight: bold;
}

.info-card-release-info p {
  margin: 0.5rem 0;
  font-size: 0.9rem; /* 20px */
  text-decoration: none;
}

.info-card-release-info a {
  margin: 0.5rem 0;
  color: #2468F2;
  font-size: 0.9rem; /* 20px */
  text-decoration: none;
}

.info-card-release-info a:hover {
  text-decoration: underline;
}
</style>
